package com.yc.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.vote.entity.Items;
import com.yc.vote.entity.Record;
import com.yc.vote.entity.Vote;
import com.yc.vote.mapper.ItemsMapper;
import com.yc.vote.mapper.RecordMapper;
import com.yc.vote.mapper.VoteMapper;
import com.yc.vote.service.BizException;
import com.yc.vote.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 投票系统-题目表-2 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-04-14
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {

    @Resource
    ItemsMapper itemsMapper;

    @Transactional // 开启事务  2张表的操作
    @Override
    public void create(Vote entity) throws BizException {
        // 字段验证  扩展: 实现 Spring 验证框架
        if( entity.getVname()==null || entity.getVname().trim().isEmpty()){
            throw new BizException("请输入标题!");
        }
        if( entity.getVtype()==null || entity.getVtype().trim().isEmpty()){
            throw new BizException("请选择类型!");
        }
        if( entity.getUsid()==null){
            throw new BizException("请登录系统!");
        }
        if(entity.getItems() == null || entity.getItems().size()<2){
            throw new BizException("至少有2个选项!");
        }
        // 同名验证: MyBatisPlus 条件查询
        // 条件构建器
        LambdaQueryWrapper<Vote> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vote::getUsid, entity.getUsid());   // usid = 100
        lqw.eq(Vote::getVname, entity.getVname());   // vname = xxxx
        long count = count(lqw);
        if(count > 0){
            throw new BizException("您已经发起过该投票!");
        }
        // 保存 vote 表记录, save 完成之后, mybatis 会返回该 记录的主键
        if (save(entity)) {
            // 保存 items 表记录
            for (Items item : entity.getItems()) {
                item.setVid(entity.getId());  //设置关联外检
                itemsMapper.insert(item);
            }
            return;
        }
        throw new BizException("新增投票失败!");
    }

    @Resource
    RecordMapper recordMapper;

    public void delById(int id) throws BizException {
        // 判断是否已经投票
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Record::getVid, id);
        Long count = recordMapper.selectCount(lqw);

        if (count>0) {
            // 已有投票: 逻辑删除  update 操作
            Vote vote = new Vote();
            vote.setId(id);
            vote.setStatus(0);
            this.updateById(vote);
        } else {
            // 未投票: 物理删除
            // 先删除 items 表记录
            LambdaQueryWrapper<Items> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(Items::getVid, id);
            itemsMapper.delete(lqw1);
            // 再删除 vote 表记录
            this.removeById(id);
        }
    }
}
